package com.emm.yixun.openapi.hander;

import net.sf.ehcache.CacheManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emm.yixun.common.app.AddCustomerReqDto;
import com.emm.yixun.common.app.AddCustomerResDto;
import com.emm.yixun.common.exception.XFUNExceptionCode;
import com.emm.yixun.customer.service.app.CustomerServiceAppFacade;
import com.emm.yixun.openapi.dto.base.BaseRequestDTO;
import com.emm.yixun.openapi.dto.base.BaseResponseDTO;
import com.emm.yixun.openapi.hander.base.CommonAbstractHander;
import com.emm.yixun.openapi.vo.AddCustomerReqVo;
import org.springframework.util.StringUtils;

/**
 * 新增客户
 *
 * @author Administrator
 */
@Service("addCustomer")
public class AddCustomerHander extends CommonAbstractHander {

    @Autowired
    private CustomerServiceAppFacade customerServiceAppFacade;
    @Autowired
    private CacheManager cacheManager;

    @SuppressWarnings("rawtypes")
    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        AddCustomerReqVo req = (AddCustomerReqVo) reqDto;
        AddCustomerReqDto reqDto1 = req.getParams();
        BaseResponseDTO<AddCustomerResDto> res = new BaseResponseDTO<AddCustomerResDto>();
        AddCustomerResDto resDto = new AddCustomerResDto();
        try {
			/*
			Cache  cache = cacheManager.getCache("activeSessionsCache");
			//获取userCode缓存对应value值
			String u_userCode = reqDto1.getMerchantId() + reqDto1.getUserId() + "userCode";//获取userCode缓存key
			Element element_userCode = cache.get(u_userCode );
			String ele_userCode = (String) element_userCode .getValue();
			//获取userCode缓存key
			String u_projectId = reqDto1.getMerchantId() + reqDto1.getUserId() + "projectId";
			Element element_projectId = cache.get(u_projectId );
			String ele_projectId= (String) element_projectId .getValue();//获取userCode缓存对应value值
			
			reqDto1.setUserCode(ele_userCode);
			reqDto1.setProjectId(Long.parseLong(ele_projectId));
			*/
            resDto = customerServiceAppFacade.addCustomer(reqDto1);
        } catch (Exception e) {
            if ("customer".equals(e.getMessage())) {
                resDto.setErrorCode(XFUNExceptionCode.UNKNOW_EXCEPTON.getCode());
                resDto.setErrorMsg("新增客户操作失败");
                resDto.setResult("0");
            } else if ("houseType".equals(e.getMessage())) {
                resDto.setErrorCode(XFUNExceptionCode.UNKNOW_EXCEPTON.getCode());
                resDto.setErrorMsg("关注户型操作失败");
                resDto.setResult("0");
            } else if ("premises".equals(e.getMessage())) {
                resDto.setErrorCode(XFUNExceptionCode.UNKNOW_EXCEPTON.getCode());
                resDto.setErrorMsg("意向组团操作失败");
                resDto.setResult("0");
            } else if ("room".equals(e.getMessage())) {
                resDto.setErrorCode(XFUNExceptionCode.UNKNOW_EXCEPTON.getCode());
                resDto.setErrorMsg("意向房号操作失败");
                resDto.setResult("0");
            } else if ("family".equals(e.getMessage())) {
                resDto.setErrorCode(XFUNExceptionCode.UNKNOW_EXCEPTON.getCode());
                resDto.setErrorMsg("家庭成员操作失败");
                resDto.setResult("0");
            } else {
                resDto.setErrorCode(XFUNExceptionCode.UNKNOW_EXCEPTON.getCode());
                resDto.setErrorMsg(XFUNExceptionCode.UNKNOW_EXCEPTON.getDesout());
                resDto.setResult("0");
            }
        }
        res.setResData(resDto);
        return res;
    }


    /**
     * 根据商户id和资源编码获取跳转页面地址
     *
     * @param merchantId 商户id
     * @param code       资源编码
     * @param type    操作类型 0新增1编辑2详情
     * @return 跳转地址
     */
    public String getRedirectPath(Long merchantId, String code, Integer type) {
        final String url;
        if(type == 0){
            url = "/customer/base/addCustomer";
        }else if(type == 1){
            url = "/customer/base/editCustomer";
        }else{
            url = "/customer/base/detail";
        }
        //传空返回默认页面
        if (merchantId == null || StringUtils.isEmpty(code)) {
            return url;
        }
        String urlStr = customerServiceAppFacade.getRedirect(merchantId, code);
        return urlStr == null ? url : urlStr;
    }

}
